package com.example.wendao.view.ui.fragment;


import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;

import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.wendao.R;

import java.lang.reflect.Field;

import scut.carson_ho.searchview.ICallBack;
import scut.carson_ho.searchview.SearchView;
import scut.carson_ho.searchview.bCallBack;


public class SearchFragment extends Fragment {

    private SearchView searchView;
    public SearchFragment() {}


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_search, container, false);
        Class<SearchView> searchClazz = SearchView.class;

        EditText mySearch;
        ImageView mySearchBack;

        searchView = view.findViewById(R.id.search_view);
        searchView.setOnClickSearch(string -> Toast.makeText(view.getContext(), "搜索方法", Toast.LENGTH_SHORT).show());

        try {
            Field esField = searchClazz.getDeclaredField("et_search");
            Field searchBack = searchClazz.getDeclaredField("searchBack");
            esField.setAccessible(true);
            searchBack.setAccessible(true);
            mySearch = (EditText) esField.get(searchView);
            mySearchBack = (ImageView) searchBack.get(searchView);
            mySearchBack.setOnClickListener(v -> Navigation.findNavController(view).navigateUp());
            esField.set(searchView, mySearch);
            searchBack.set(searchView, mySearchBack);
        } catch (Exception e){
            e.printStackTrace();
        }
        return view;
    }

}
